//
//  DXWNavigationController.m
//  BuDeJie
//
//  Created by 淡馨慰 on 16/7/28.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWNavigationController.h"

@interface DXWNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation DXWNavigationController

/**
 *  设置导航条标题字体 -- 设置所有appearance
 */
+(void)load
{
    /**
     *  设置导航条标题字体
     */
    
    //appearance太暴力,不太好,只获取指定类中的全部属性比较好
//    UINavigationBar *navBar = [UINavigationBar appearance];
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    //方式一:UINavigationBar的方法:方式二属性的setter方法
//    [navBar setTitleTextAttributes:attr];
    //方式二:UINavigationBar的属性
    navBar.titleTextAttributes = attr;
    
    /**
     *  设置导航条背景图片
     */
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//        NSLog(@"VC:%@---%ld",[viewController class],self.childViewControllers.count);
    if (self.childViewControllers.count > 0 ) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        //@selector(popViewControllerAnimated:)
        [btn addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
//        [btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        btn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
        
        viewController.navigationItem.leftBarButtonItem = backBtn;
    }

    [super pushViewController:viewController animated:animated];

}
- (void)backBtnClick
{

    NSLog(@"%@点击了返回按钮",[self class]);
    [self popViewControllerAnimated:YES];
//    [super popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**
     *  设置滑动返回
     */

    /*
     2016-07-29 10:22:52.987 BuDeJie[1343:55326] 左侧返回代理<_UINavigationInteractiveTransition: 0x7ff232e37b10>
     2016-07-29 10:22:52.987 BuDeJie[1343:55326] 左侧返回手势<UIScreenEdgePanGestureRecognizer: 0x7ff232e37cb0; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7ff232e36310>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7ff232e37b10>)>>
     */

    
    //自定义滑动手势,将滑动范围扩大,而不只是边缘滑动 -- 全屏滑动
    //禁用系统的手势
    self.interactivePopGestureRecognizer.enabled = NO;
    //强行获取系统的interactivePopGestureRecognizer的代理
    id target = self.interactivePopGestureRecognizer.delegate;
    //自定义一个滑动手势,让它执行interactivePopGestureRecognizer代理执行的方法handleNavigationTransition:
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:target action:@selector(handleNavigationTransition:)];
    //给每个导航控制器添加这个滑动手势
    [self.view addGestureRecognizer:panGesture];
    //让控制器管理这个 自定义的全屏滑动返回手势
    panGesture.delegate = self;
    
//    
//    NSLog(@"左侧返回代理%@",self.interactivePopGestureRecognizer.delegate);
//    NSLog(@"左侧返回手势%@",self.interactivePopGestureRecognizer);
    
}

/**
 *  设置滑动返回
 */
#pragma mark - UIGestureRecognizerDelegate
//每次触发手势的时候,询问代理,是否要触发手势,YES:触发 -- 有滑动返回,出现假死,NO:不触发 -- 无滑动返回,出现假死
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count > 1;
}
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
//{
////    return NO;
//    return YES;
//}



@end
